package com.waimai.db.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.waimai.db.dao.OrderDao;
import com.waimai.db.dao.RestaurantDao;
import com.waimai.db.entity.Comment;
import com.waimai.db.entity.Food;
import com.waimai.db.entity.Order;
import com.waimai.db.entity.OrderFood;
import com.waimai.db.entity.OrderStatus;
import com.waimai.db.entity.PayMode;
import com.waimai.db.entity.Restaurant;
import com.waimai.db.service.RestaurantService;

@Service
public class RestaurantServiceImpl implements RestaurantService{

	@Autowired
	public RestaurantDao restaurantDao;
	
	@Autowired
	public OrderDao orderDao;
	
	public List<Restaurant> findBykeyWords(String keyWords) {
		return restaurantDao.findBykeyWords(keyWords);
	}

	public List<Food> getFoods(Integer restaurantId) {
		return restaurantDao.getFoods(restaurantId);
	}

	public Restaurant findById(Integer restaurantId) {
		return restaurantDao.findById(restaurantId);
	}

	public Order addOrder(Order order) {
		order.getReceiveaddress().setAddressId(null);
		order.getReceiveaddress().setUser(null);
		order.setReceiveaddress(restaurantDao.addAddress(order.getReceiveaddress()));
			
		order.setPaymode(new PayMode(PayMode.PAY_MODE_DEFAULT));
		order.setOrderstatus(new OrderStatus(OrderStatus.STATUS_WAIT_CONFIRM));
		restaurantDao.addOrder(order);
		
		for(OrderFood orderFood : order.getOrderfoods()){
			orderFood.setOrder(order);
			orderDao.addOrderFood(orderFood);
		}
		return order;
	}

	public OrderStatus updateOrderSatus(Integer orderId, OrderStatus orderStatus) {
		return orderDao.updateStatus(orderId, orderStatus);
	}

	public List<Comment> getComments(Integer restaurantId, int commentLevel) {
		List<Comment> comments = null;
		
		switch (commentLevel) {
		case Comment.COMMENT_ALL:
			comments = restaurantDao.getComments(restaurantId, 0, Comment.STARS_MAX);
			break;
		case Comment.COMMENT_GOOD:
			comments = restaurantDao.getComments(restaurantId, Comment.STARS_GOOD, Comment.STARS_MAX);
			break;
		case Comment.COMMENT_MIDDLE:
			comments = restaurantDao.getComments(restaurantId, Comment.STARS_MIDDLE, Comment.STARS_GOOD);
			break;
		case Comment.COMMENT_BAD:
			comments = restaurantDao.getComments(restaurantId, 0, Comment.STARS_MIDDLE);
			break;
		}
		return comments;
	}

	public List<Restaurant> getRestaurants() {
		return restaurantDao.getRestaurants();
	}

	public void save(Restaurant restaurant) {
		restaurantDao.save(restaurant);
	}

	public void update(Restaurant restaurant) {
		restaurantDao.update(restaurant);
	}

	public List<Restaurant> getRestaurantsByAdminId(int adminId) {
		return restaurantDao.getRestaurantsByAdminId(adminId);
	}

	public Restaurant findByRestuarentName(String restaurantName) {
		return restaurantDao.findByRestuarentName(restaurantName);
	}

	public List<Comment> getCommentByAdminId(int adminId) {
		return restaurantDao.getCommentByAdminId(adminId);
	}

	public Restaurant getRestaurantsByName(String restaurantName) {
		return restaurantDao.getRestaurantsByName(restaurantName);
	}

}
